#include "StdAfx.h"
#include <algorithm>

using namespace std;

#include "StationModel.h"
#include "StationDlg.h"


//#define FILENAME "E:\\sccstm\\trunk\\debug\\station.ini"
#define SNAME	"name"
#define SID	"id"
#define SIP		"ipaddress"
#define SPANEL	"panel"


CStationModel::CStationModel(void)
{
}

CStationModel::~CStationModel(void)
{
}
void CStationModel::deleteStation(const CString & sid)
{
	vector <station>::iterator Iter;

	for ( Iter = list.begin( ) ; Iter != list.end( ) ; Iter++ )
    {
		if(Iter->ID == sid)
		{
			list.erase(Iter);
			break;
		}
	}
}
void CStationModel::addStation(station s)
{
	list.push_back(s);
	CstationDlg * pdlg = static_cast<CstationDlg*>(::AfxGetMainWnd());
	pdlg->addStationToTree( s,m_view->GetRootItem() );
	

}
void CStationModel::loadStation(void)
{
	CString section;
	CString szbuf;
	CString szbinpath ;
	TCHAR buf[MAX_PATH] ={ 0 };
	::GetModuleFileName(NULL,buf,MAX_PATH);
	szbinpath = buf;
	szbinpath = szbinpath.Mid(0,szbinpath.ReverseFind('\\'));
	szbinpath.Append("\\station.ini");
	station sta;
	for ( int i = 1;;i ++)
	{
		char buf[80] = { 0 };
		section.Format(_T("station%d"),i);
		GetPrivateProfileString( section,_T(SID),_T("@@null"),buf,80,szbinpath );
		szbuf = buf;
		if ( szbuf == "@@null" )
		{
			break;
		}
		sta.ID = buf;
		GetPrivateProfileString( section,_T(SNAME),_T("@@null"),buf,80,szbinpath );
		if((szbuf = buf ) != "@@null")
		{	
			sta.name = buf;
		}
		GetPrivateProfileString( section,_T(SIP),_T("@@null"),buf,80, szbinpath );
		if((szbuf = buf ) != "@@null")
		{	
			sta.ip = szbuf;
		}
		GetPrivateProfileString( section,_T(SPANEL),_T("@@null"),buf,6,szbinpath );
		if((szbuf = buf ) != "@@null")
		{	
			memcpy(sta.panel,buf,6);
		}
		//insert into vector
		list.push_back(sta);	
	}
}
void CStationModel::saveStation(void)
{
	CString szbinpath ;
	TCHAR buf[MAX_PATH] ={ 0 };
	::GetModuleFileName(NULL,buf,MAX_PATH);
	szbinpath = buf;
	szbinpath = szbinpath.Mid(0,szbinpath.ReverseFind('\\'));
	szbinpath.Append("\\station.ini");
	::DeleteFile(szbinpath);
	station s;
	CString section;
	for(unsigned int i=0;i<list.size();i++)
	{
		
		s = list.at(i);
		section.Format(_T("station%d"),i+1);
		::WritePrivateProfileString(section,SNAME,s.name,szbinpath);
		::WritePrivateProfileString(section,SID,s.ID,szbinpath);
		WritePrivateProfileString(section,SIP,s.ip,szbinpath);
		CString pa( s.panel,5);
		::WritePrivateProfileString(section,SPANEL,pa,szbinpath);
		
	}
}

BOOL CStationModel::getStation(station & s, unsigned int index)
{
	if(index < list.size())
	{
		s = list.at(index);
		return TRUE;
	}
	return FALSE;
}

bool CStationModel::check(const CString & sid)
{
	for(int i=0;i < list.size();i++)
	{
		if(list.at(i).ID == sid)
			return false;
	}

	return true;
}

CString CStationModel::getPanelByID(const CString& staID)
{
	for(int i=0;i < list.size();i++)
	{
		if(list.at(i).ID == staID)
			return list.at(i).panel;
	}

	return CString();
}

station CStationModel::getStationByID(CString sid)
{

	for(int i=0;i < list.size();i++)
	{
		if(list.at(i).ID == sid)
			return list.at(i);
	}
	return station();
}
